package h0722;

import java.util.Arrays;
import java.util.Scanner;
/**
  * @description 求幸存数之和
  * @author 不知名帅哥
  * @date 2024/7/22 20:29
  * @version 1.0
*/
public class FindSurvivors2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int jump = sc.nextInt(); //跳数
        int m = sc.nextInt();  //幸存的数量
        int result = getResult(array, jump, m);
        System.out.println(result);
    }

    private static int getResult(int[] array, int jump, int m) {
        CycleLinkedList cycleLinkedList = new CycleLinkedList();
        for (int i : array) {
            cycleLinkedList.add(i);
        }
        Node cur=cycleLinkedList.head.next;  //必须从开始节点的下一个节点开始，要不然会出问题。
        while (cycleLinkedList.size>m){
            for (int i = 0; i <= jump; i++) {
                if (i==jump){
                    cur=cycleLinkedList.remove(cur);
                }else {
                    cur=cur.next;
                }
            }
        }
        int sum=0;
        for (int i = 0; i < cycleLinkedList.size; i++) {
            sum+=cur.val;
            cur=cur.next;
        }
        return sum;
    }

    static class Node {
        int val;
        Node prev; //前驱
        Node next; //后继

        public Node(int val) {
            this.val = val;
        }
    }

    static class CycleLinkedList {
        private int size; //长度
        private Node head; //头节点
        private Node tail; //尾节点

        public CycleLinkedList() {
            this.size=0;
            this.head=null;
            this.tail=null;
        }
        public void add(int val){
            Node node=new Node(val);
            if (head==null){
                head=node;
                tail=node;
                head.next=head;
                head.prev=head;
            }else {
                node.next=head;
                head.prev=node;

                tail.next=node;
                node.prev=tail;

                tail=node; //node变为tail
            }
            this.size++;
        }

        public Node remove(Node node){
            Node p = node.prev;
            Node n = node.next;
            p.next=n;
            n.prev=p;

            node.prev=null;
            node.next=null;

            if (head==node){
                head=null;
                this.head=n;
            }
            if (tail==node){
                this.tail=p;
            }

            this.size--;
            return n;
        }

        public int getSize(){
            return this.size;
        }
    }
}
